<template>
    <div class="layerPublicStyle">
        <lay-form-item label="订单编号"  class="_row_style" >
            <div class="_content">
                {{  formData.order_id }}
            </div>
        </lay-form-item>
        <lay-form-item label="所属学校" class="_row_style" >
            <div class="_content">
                {{  formData.school_id[0].text }}
            </div>
        </lay-form-item>
        <lay-form-item label="所属商户" class="_row_style" v-if="formData.refund_module==='foodTakeout'">
            <div class="_content">
                {{  formData.merchant_id[0].text }}
            </div>
        </lay-form-item>
        <lay-form-item label="订单总金额" class="_row_style">
            <div class="_content">
                <div class="_money_color">
                    <span v-if="formData.refund_module==='foodTakeout'">
                        {{  +getBigJs(formData.order_real_all_money).div(100) }}
                    </span>
                    <span v-else-if="formData.refund_module==='runErrand'">
                        {{  +getBigJs(formData.order_all_money) }}
                    </span>
                </div> 元
            </div>
        </lay-form-item>
        <lay-form-item label="退款金额" class="_row_style layRequired"  style="margin-bottom:20px">
            <lay-input type="number" :allow-clear="true" v-model="formData.refund_amount" style="width: 300px;"></lay-input>
            <div class="_flex alignItems" style="font-size:12px;margin-top:3px">
                (最多 <div 
                        style="padding: 0px 2px;font-weight: bold;"
                        class="_money_color"
                    >
                    <span v-if="formData.refund_module==='foodTakeout'">
                        {{ +getBigJs(formData.order_real_all_money).div(100)  }}
                    </span>
                    <span v-if="formData.refund_module==='runErrand'">
                        {{ +getBigJs(formData.order_all_money) }}
                    </span>
                </div> 元)
            </div>
        </lay-form-item>
        <lay-form-item label="退款原因" class="_row_style" >
            <lay-textarea placeholder="" style="width: 300px;" v-model="formData.refund_remark" :maxlength="80"></lay-textarea>
            <div class="_flex alignItems" style="font-size:12px;color:#999;margin-top:3px">非必填信息，若填写将展示在下发给用户的退款消息中</div>
        </lay-form-item>
    </div>
</template>

<script setup>
import {layAddRequiredIcon} from '@/common/layer-com-utils.js'
import { reactive,nextTick,onMounted,ref} from 'vue'
import BigJs from 'big.js'
import {checkMoneyAmount} from '~@/utils/helper.js'

const adminWxPay = uniCloud.importObject('admin-wx-pay',{
    customUI:true
}) 

const props = defineProps({
    serverData:Object,
    callback:Function
})

const formData=reactive({
    ...props.serverData,
    refund_amount:``,
    refund_remark:``
});

    onMounted(async () => {
        layAddRequiredIcon();
        props.callback([
            parentCallVerify,
        ]);
    })
    
    function getBigJs(args){
        return new BigJs(args)
    }

    function parentCallVerify(){
        return new Promise(async resolve=>{
            const { myErrCode,errMsg} =checkMoneyAmount(formData.refund_amount);
            if(myErrCode!==0){
                return layer.msg(errMsg, { icon : 2, time: 1500})
            }
            if(formData.refund_module==='foodTakeout'){
                if(getBigJs(formData.refund_amount).times(100).gt(formData.order_real_all_money)){
                    return layer.msg(`最多只能退款 ${ +getBigJs(formData.order_real_all_money).div(100)}元`, { icon : 2, time: 1500})
                }
            }else if(formData.refund_module==='runErrand'){
                if(getBigJs(formData.refund_amount).gt(formData.order_all_money)){
                    return layer.msg(`最多只能退款 ${ +getBigJs(formData.order_all_money)}元`, { icon : 2, time: 1500})
                }
            }

            const isSure= await new Promise((next)=>{
                layer.confirm(`你确定为该订单退款 ${+getBigJs(formData.refund_amount)} 元？`, {
                    title:`操作提示`,
                    area:[`350px`,`170px`],
                    icon:3,
                    btn: [{
                        text:`确定`,
                        callback:async (layerId)=>{
                            next(1);
                            layer.close(layerId);
                        }
                    },{
                        text:`取消`,
                        callback:(layerId)=>{
                            next(0)
                            layer.close(layerId);
                        }
                    }]
                }) 
            }) 
            if(isSure==0){
                return false
            }

            uni.showLoading({
                title: `正在处理`,
                mask:true
            });
            try {
                const results = await adminWxPay.startWxRefund({
                    amount:formData.refund_amount,
                    remark:formData.refund_remark,
                    order_id:formData.order_id,
                    refund_module:formData.refund_module
                })
                uni.hideLoading()
                if(results.myErrCode!==0){
                    return layer.msg(results.errMsg,{time: 1500,icon:2})
                }
                layer.msg(`处理成功`, { time: 1500,icon:1 })
                return resolve(1);  //返回1 表示刷新页面
            } catch (error) {
                console.error(`xxxxxx ====> 发起退款失败，失败原因如下：<====xxxxxx`)
                console.error(error)
            }
            uni.hideLoading()
            layer.msg(`系统繁忙，请稍后再试`,{time: 1500,icon:2})
        })
    }

</script>

<style lang="stylus" scoped>
.layerPublicStyle
    padding 20px
</style>


